package com.stonedt.spider.controller.government;

import java.beans.Encoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stonedt.spider.util.DateUtil;
import com.stonedt.spider.util.ElasticSearchUtil;
import com.stonedt.spider.util.UtilConfig;
import com.sun.xml.internal.bind.v2.runtime.output.Encoded;

@Controller
@RequestMapping("/governmentsector")
public class GovernmentsectorController {
	
	
	/***
	 * 中央政府
	 * @param mav
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/centralgovernment",method = RequestMethod.GET)
	public ModelAndView centralgovernment(ModelAndView mav) {
			mav.addObject("nowArray", DateUtil.getNowDate());
			mav.setViewName("government/centralgovernment_list");
		return mav;
	}

	
	@RequestMapping(value="/centralgovernment-data",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject centralgovernment2(
			@RequestParam(value = "matchingmode") String matchingmode,
			@RequestParam(value = "searchWord") String searchWord,
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page) {
		JSONObject json=new JSONObject();
		try {
			int searchType=0;
			String response="";
			String param="";
			switch (matchingmode) {
			case "全部":
				searchType=0;
				break;
			case "标题":
				searchType=1;
				break;
			case "正文":
				searchType=2;
				break;
			}
			param="?&page="+page+"&size=10&otherseedid=22950,23008&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;	
			Integer count =0;
			Integer page_count =0;
			Integer size =0;
				String api = ElasticSearchUtil.yysQbsearchcontent;
				response = ElasticSearchUtil.sendPost1(UtilConfig.getURL()+api, param);
				json = JSONObject.parseObject(response);
				JSONArray news=json.getJSONArray("data");
				count=Integer.valueOf(json.getString("count"));
				page_count=Integer.valueOf(json.getString("page_count"));
				size=Integer.valueOf(json.getString("size"));
			json.put("news",news);
			json.put("count",count);
			json.put("page_count",page_count);
			json.put("page",page);
			json.put("size",size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
	/***
	 * 地方政府
	 * @param mav
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/localgovernment",method = RequestMethod.GET)
	public ModelAndView localgovernment(ModelAndView mav) {
			mav.addObject("nowArray", DateUtil.getNowDate());
			mav.setViewName("government/localgovernment_list");
		return mav;
	}

	
	@RequestMapping(value="/localgovernment-data",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject localgovernment2(
			@RequestParam(value = "matchingmode") String matchingmode,
			@RequestParam(value = "searchWord") String searchWord,
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page) {
		JSONObject json=new JSONObject();
		try {
			int searchType=0;
			String response="";
			String param="";
			switch (matchingmode) {
			case "全部":
				searchType=0;
				break;
			case "标题":
				searchType=1;
				break;
			case "正文":
				searchType=2;
				break;
			}
			param="?&page="+page+"&size=10&otherseedid=22950,23008,22954,22956,22959,22960,22961,22963,22964,22981,22999,23010,23012,23014,23015,23017,23020,23021,23023,23024,23025,23026,23029,23030,23032,23034,23035,23037,23039&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;	
			Integer count =0;
			Integer page_count =0;
			Integer size =0;
				String api = ElasticSearchUtil.yysQbsearchcontent;
				response = ElasticSearchUtil.sendPost1(UtilConfig.getURL()+api, param);
				json = JSONObject.parseObject(response);
				JSONArray news=json.getJSONArray("data");
				count=Integer.valueOf(json.getString("count"));
				page_count=Integer.valueOf(json.getString("page_count"));
				size=Integer.valueOf(json.getString("size"));
			json.put("news",news);
			json.put("count",count);
			json.put("page_count",page_count);
			json.put("page",page);
			json.put("size",size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
	
	/***
	 * 中央政策
	 * @param mav
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/centralpolicy",method = RequestMethod.GET)
	public ModelAndView centralpolicy(ModelAndView mav,
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page) {
			mav.addObject("nowArray", DateUtil.getNowDate());
			mav.setViewName("government/centralpolicy_list");
		return mav;
	}

	
	@RequestMapping(value="/centralpolicy-data",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject centralpolicy2(
			@RequestParam(value = "matchingmode") String matchingmode,
			@RequestParam(value = "searchWord") String searchWord,
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page) {
		JSONObject json=new JSONObject();
		try {
			int searchType=0;
			String response="";
			String param="";
			switch (matchingmode) {
			case "全部":
				searchType=0;
				break;
			case "标题":
				searchType=1;
				break;
			case "正文":
				searchType=2;
				break;
			}
			param="?&page="+page+"&size=10&otherseedid=23081&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
			Integer count =0;
			Integer page_count =0;
			Integer size =0;
			String api = ElasticSearchUtil.yysQbsearchcontent;
			response = ElasticSearchUtil.sendPost1(UtilConfig.getURL()+api, param);
				json = JSONObject.parseObject(response);
				JSONArray news=json.getJSONArray("data");
				count=Integer.valueOf(json.getString("count"));
				page_count=Integer.valueOf(json.getString("page_count"));
				size=Integer.valueOf(json.getString("size"));
			json.put("news",news);
			json.put("count",count);
			json.put("page_count",page_count);
			json.put("page",page);
			json.put("size",size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
	/***
	 * 地方政策
	 * @param mav
	 * @param page
	 * @return
	 */
	
	@RequestMapping(value="/localpolicy",method = RequestMethod.GET)
	public ModelAndView localpolicy(ModelAndView mav,
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page) {
			mav.addObject("nowArray", DateUtil.getNowDate());
			mav.setViewName("government/localpolicy_list");
		return mav;
	}
	
	@RequestMapping(value="/localpolicy-data",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject localpolicy2(
			@RequestParam(value = "matchingmode") String matchingmode,
			@RequestParam(value = "searchWord") String searchWord,
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page) {
		JSONObject json=new JSONObject();
		try {
			int searchType=0;
			String response="";
			String param="";
			switch (matchingmode) {
			case "全部":
				searchType=0;
				break;
			case "标题":
				searchType=1;
				break;
			case "正文":
				searchType=2;
				break;
			}
			param="?&page="+page+"&size=10&otherseedid=23085&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
			Integer count =0;
			Integer page_count =0;
			Integer size =0;
			String api = ElasticSearchUtil.yysQbsearchcontent;
			response = ElasticSearchUtil.sendPost1(UtilConfig.getURL()+api, param);
				json = JSONObject.parseObject(response);
				JSONArray news=json.getJSONArray("data");
				count=Integer.valueOf(json.getString("count"));
				page_count=Integer.valueOf(json.getString("page_count"));
				size=Integer.valueOf(json.getString("size"));
			json.put("news",news);
			json.put("count",count);
			json.put("page_count",page_count);
			json.put("page",page);
			json.put("size",size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
//	全国人大  全国政协 最高检察院 最高法院
	@RequestMapping( value = "/governmentInfo", method = RequestMethod.GET)
    public ModelAndView togovernmentInfo(ModelAndView mv, @RequestParam("type") String type,
    		HttpServletRequest request) {
        mv.addObject("type", type);
        if(type.equals("npc")) {
        	mv.addObject("webName", "全国人大");
        	request.setAttribute("LEFT", "npc");
        }else if(type.equals("cppcc")) {
        	mv.addObject("webName", "全国政协");
        	request.setAttribute("LEFT", "cppcc");
        }else if(type.equals("procuratorate")) {
        	mv.addObject("webName", "最高检察院");
        	request.setAttribute("LEFT", "procuratorate");
        }else if(type.equals("supremecourt")) {
        	mv.addObject("webName", "最高法院");
        	request.setAttribute("LEFT", "supremecourt");
        }else if(type.equals("learningpower")) {
        	mv.addObject("webName", "学习强国");
        	request.setAttribute("LEFT", "learningpower");
        }else if(type.equals("shzf")) {
        	mv.addObject("webName", "苏沪政府");
        	request.setAttribute("LEFT", "shzf");
        }else if(type.equals("kjbm")) {
        	mv.addObject("webName", "科技部门");
        	request.setAttribute("LEFT", "kjbm");
        }else if(type.equals("zky")) {
        	mv.addObject("webName", "中科院");
        	request.setAttribute("LEFT", "zky");
        }else if(type.equals("gdyx")) {
        	mv.addObject("webName", "高等院校");
        	request.setAttribute("LEFT", "gdyx");
        }
        mv.addObject("nowArray", DateUtil.getNowDate());
        mv.setViewName("government/study");
        return mv;
    }
	
	
	@RequestMapping(value="/governmentInfo-data",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject governmentInfoData(
			@RequestParam(value = "matchingmode") String matchingmode,
			@RequestParam(value = "searchWord") String searchWord,
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page,
			@RequestParam("type") String type) {
		JSONObject json=new JSONObject();
		try {
			int searchType=0;
			String response="";
			String param="";
			switch (matchingmode) {
			case "全部":
				searchType=0;
				break;
			case "标题":
				searchType=1;
				break;
			case "正文":
				searchType=2;
				break;
			}
			if(type.equals("npc")) {//全国人大
				param="?&page="+page+"&size=10&otherseedid=23169,23170,23171,23172&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
	        }else if(type.equals("cppcc")) {//全国政协
	        	param="?&page="+page+"&size=10&otherseedid=23192,23193&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
	        }else if(type.equals("procuratorate")) {//最高检察院
	        	param="?&page="+page+"&size=10&otherseedid=23173,23174,23175,23194,23203,23245,23246,23247,23248,23249,23250,23251,23252,23253,23254,23255,23256,23257,23258,23259,23260,23261,23262,23263,23264,23265,23266,23267,23268,23269,23270,23271,23272,23273,23274,23275,23276&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
	        }else if(type.equals("supremecourt")) {//最高法院
	        	param="?&page="+page+"&size=10&otherseedid=23176,23177,23178,23179&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
	        }else if(type.equals("learningpower")) {//学习强国
	        	param="?&page="+page+"&size=10&otherseedid=23184,23185,23186,23187,23188,23189,23190,23191&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
	        }else if(type.equals("shzf")){//苏沪政府
	        	param="?&page="+page+"&size=10&otherwebsiteid=1223,1222,1306,1307,1308,1309,1310,1311,1312,1313,1314,1315,1316,1317&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
	        }else if(type.equals("kjbm")){//科技部门
	        	param="?&page="+page+"&size=10&otherwebsiteid=1325,1332,1333,1334,1335,1336,1337,1338,1326,1223,1323,1339,1340,1341,1342,1343,1344,1345,1346,1347,1348,1349,1350,1351,1352,1353,1354,1355,1356,1357,1358,1359,1360,1361,1362,1363&stopseedid=999999999,999999998&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
	        }else if(type.equals("zky")){//中科院
	        	param="?&page="+page+"&size=10&otherwebsiteid=465,1327,1328,1365,1330,1365,1366,1367,1368,1369,1370,1371,1372,1373,1374,1375,1376,1377,1378,1379,1380,1381,1382,1383,1384,1385,1386,1387,1388,1389,1390,1391,1392,1393,1394,1395,1396,1397,1398,1399,1400,1401,1402,1403,1404,1405,1406,1407,1408,1409,1410,1411,1412,1413,1414,1415,1416,1417,1418,1419,1420,1421,1422,1423,1424,1425,1426,1427,1428,1429,1430,1431,1432,1433,1434,1435,1436,1437,1438,1439,1440,1441,1442,1443,1444,1445,1446,1447,1448,1449,1450,1451,1452,1453,1454,1455,1456,1457,1458,1459,1460,1461,1462,1463,1464,1465,1466,1467,1468,1469,1470,1471,1472,1473,1474,1475,1476,1477,1478,1479,1480,1481,1482,1483,1484,1485,1486&stopseedid=999999999,999999998&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
	        }else if(type.equals("gdyx")){//高等院校
	        	param="?&page="+page+"&size=10&otherwebsiteid=1488,1489,1490,1491,1492,1493,1494,1495,1496,1497,1498,1499,1500,1501,1502,1503,1504,1505,1506,1507,1508,1509,1510,1511,1512,1513,1514,1515,1516,1517,1518,1519,1520,1521,1522,1523,1524,1525,1526,1527,1528,1529,1530,1531,1532,1533,1534,1535,1536,1537,1538,1539,1540,1541,1542,1543,1544,1545,1546,1547,1548,1549,1550,1551,1553,1554,1555,1558,1559,1560,1561,1562,1656,1657,1658,1659,1660,1663,1665,1666,1667,1668,1669,1670,1671,1672,1673,1674,1675,1676,1677,1678,1679,1680,1681,1682,1683,1684,1686,1687,1688,1690,1691,1694,1699,1700,1701,1703,1704,1705,1706,1556,1708,1709,1710,1711,1712,1713,1714,1715,1716,1717,1718,1719,1720,1721&stopseedid=999999999,999999998&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
	        }
			Integer count =0;
			Integer page_count =0;
			Integer size =0;
			String api = ElasticSearchUtil.yysQbsearchcontent;
			response = ElasticSearchUtil.sendPost1(UtilConfig.getURL()+api, param);
				json = JSONObject.parseObject(response);
				JSONArray news=json.getJSONArray("data");
				count=Integer.valueOf(json.getString("count"));
				page_count=Integer.valueOf(json.getString("page_count"));
				size=Integer.valueOf(json.getString("size"));
			json.put("news",news);
			json.put("count",count);
			json.put("page_count",page_count);
			json.put("page",page);
			json.put("size",size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
	/***
	 * 跳转到详情页面
	 * @param mv
	 * @param article_public_id
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/governmentInfo-details",method = RequestMethod.POST)
	@ResponseBody
	public ModelAndView governmentInfoDetails(
			ModelAndView mv, @RequestParam("article_public_id") String article_public_id,
			@RequestParam("type") String type,
    		HttpServletRequest request) {
		if(type.equals("npc")) {
        	mv.addObject("webName", "全国人大");
        	request.setAttribute("LEFT", "npc");
        }else if(type.equals("cppcc")) {
        	mv.addObject("webName", "全国政协");
        	request.setAttribute("LEFT", "cppcc");
        }else if(type.equals("procuratorate")) {
        	mv.addObject("webName", "最高检察院");
        	request.setAttribute("LEFT", "procuratorate");
        }else if(type.equals("supremecourt")) {
        	mv.addObject("webName", "最高法院");
        	request.setAttribute("LEFT", "supremecourt");
        }else if(type.equals("learningpower")) {
        	mv.addObject("webName", "学习强国");
        	request.setAttribute("LEFT", "learningpower");
        }
		mv.setViewName("government/study_details");
		return mv;
	}
	
	/***
	 * 获取详情页面的数据
	 * @param mv
	 * @param article_public_id
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/Infodetails-data",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject InfodetailsData(@RequestParam("article_public_id") String article_public_id,
			@RequestParam("type") String type) {
		//暂时不做详情
		return null;
	}
	
	

	/***
	 * 宏观数据
	 * @param mav
	 * @param page
	 * @return
	 */
	
	@RequestMapping(value="/nationalData",method = RequestMethod.GET)
	public ModelAndView nationalData(ModelAndView mav,
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page) {
			
			mav.addObject("nowArray", DateUtil.getNowDate());
			mav.setViewName("government/nationalData");
		return mav;
	}
	/***
	 * 宏观数据 分类数据请求
	 * @param mav
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/nationalData-list",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject nationalData_list(
			@RequestParam(value = "category",required = false)String category,
			@RequestParam(value = "recategory",required = false)String recategory) {
		JSONObject json=new JSONObject();
		try {
			String response="";
			String param="";
			String api="macrodata/macrodatacategory";
			if(category.equals("")==false) {
				param="?&category="+URLEncoder.encode(category, "utf-8");
				api="macrodata/macrodatarecategory";
			}
			if(recategory.equals("")==false) {
				param="?&category="+URLEncoder.encode(category, "utf-8")+"&recategory="+URLEncoder.encode(recategory, "utf-8");
				api="macrodata/macrodatacompare";
			}
				response = sendPost(api,param);
				System.err.println(response);
				json = JSONObject.parseObject(response);
				JSONArray buckets=json.getJSONObject("aggregations").getJSONObject("group_by_tags").getJSONArray("buckets");
			json.put("buckets",buckets);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
	
	/***
	 * 宏观数据 详细数据请求
	 * @param mav
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/nationalData-details",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject nationalData_details(
			@RequestParam(value = "category", defaultValue = "" ,required = false)String category,
			@RequestParam(value = "recategory", defaultValue = "" ,required = false)String recategory,
			@RequestParam(value = "compare_theway", defaultValue = "" ,required = false)String compare_theway) {
		/***
		 * category  一级分类
		 * recategory 二级分类
		 * compare_theway 三积分类
		 */
		JSONObject json=new JSONObject();
		try {
			String response="";
			String param="?category="+convert(category)+"&recategory="+convert(recategory)+"&compare_theway="+convert(compare_theway);
			//System.out.println(param);
			String api="macrodata/macrodatalist";
				response = sendPost(api,param);
				json = JSONObject.parseObject(response);
				System.out.println(response);
				JSONArray buckets=json.getJSONArray("data");
				JSONArray endAraay=new JSONArray();
				for(int i=0;i<buckets.size();i++) {
					String data=buckets.getJSONObject(i).getJSONObject("_source").get("data").toString();
					JSONObject dataOBJ=JSONObject.parseObject(data);
					endAraay.add(dataOBJ);
				}
			json.put("endAraay",endAraay);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
	
	/***
	 * 国家机关
	 * @param mav
	 * @param page
	 * @return
	 */
	
	@RequestMapping(value="/nationalOrgan",method = RequestMethod.GET)
	public ModelAndView nationalOrgan(ModelAndView mav) {
			mav.addObject("nowArray", DateUtil.getNowDate());
			mav.setViewName("government/nationalOrgan");
		return mav;
	}
	/***
	 * 国家机关数据请求
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/nationalOrgan-data",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject nationalOrgan_data(
			@RequestParam(value = "matchingmode") String matchingmode,
			@RequestParam(value = "searchWord") String searchWord,
			@RequestParam(value = "page", defaultValue = "1" ,required = false)Integer page) {
		JSONObject json=new JSONObject();
		try {
			int searchType=0;
			String response="";
			String param="";
			switch (matchingmode) {
			case "全部":
				searchType=0;
				break;
			case "标题":
				searchType=1;
				break;
			case "正文":
				searchType=2;
				break;
			}
			//央行466   国家发改委网站 852     财政部417 商务部419 外交部424 教育部425 科技部431 公安部436 明正不440 司法部447 人社部451 住建部452 交通部454 水利部455 农业农村部456 文化和旅游部457 自然资源部477 生态环境部478 国防部479 退役军人事务部525 应急管理部526 卫生部网站585  国资委 422 国家民委 434 卫健委 524 国家发改委网站 852
			param="?&page="+page+"&size=10&otherwebsiteid=466,852,417,419,424,425,431,436,440,447,451,452,454,455,456,457,477,478,479,525,526,585,422,434,524,852&searchType=1&matchingmode="+searchType+"&keyword="+searchWord;
			Integer count =0;
			Integer page_count =0;
			Integer size =0;
			String api = ElasticSearchUtil.yysQbsearchcontent;
			response = ElasticSearchUtil.sendPost1(UtilConfig.getURL()+api, param);
				json = JSONObject.parseObject(response);
				JSONArray news=json.getJSONArray("data");
				count=Integer.valueOf(json.getString("count"));
				page_count=Integer.valueOf(json.getString("page_count"));
				size=Integer.valueOf(json.getString("size"));
			json.put("news",news);
			json.put("count",count);
			json.put("page_count",page_count);
			json.put("page",page);
			json.put("size",size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	public static String sendPost(String method, String param) throws Exception {
        String url = UtilConfig.getURL2()+ method;
        PrintWriter out = null;
        BufferedReader in = null;
        System.out.println(url + param);
        URL realUrl = new URL(url);
        URLConnection conn = realUrl.openConnection();
        // 打开和URL之间的连接
        //URLConnection conn = realUrl.openConnection();
        // 设置通用的请求属性
//			conn.setRequestProperty("content-type","application/json; charset=UTF-8");

        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        // 发送POST请求必须设置如下两行
        conn.setDoOutput(true);
        conn.setDoInput(true);
        // 获取URLConnection对象对应的输出流
        out = new PrintWriter(conn.getOutputStream());
        // 发送请求参数
        out.print(param);
        // flush输出流的缓冲
        out.flush();
        // 定义BufferedReader输入流来读取URL的响应
        in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));//转成utf-8格式
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = in.readLine()) != null) {
            response.append(line);
        }
        // 使用finally块来关闭输出流、输入流
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return response.toString();
    }
	
	/** 
     * 将字符串转成unicode 

     * @param str 待转字符串 
     * @return unicode字符串 
     */ 
    public static String convert(String str) { 
        str = (str == null ? "" : str); 
        String tmp; 
        StringBuffer sb = new StringBuffer(1000); 
        char c; 
        int i, j; 
        sb.setLength(0); 
        for (i = 0; i < str.length(); i++) { 
            c = str.charAt(i); 
            sb.append("\\u"); 
            j = (c >>>8); //取出高8位 
            tmp = Integer.toHexString(j); 
            if (tmp.length() == 1) 
                sb.append("0"); 
            sb.append(tmp); 
            j = (c & 0xFF); //取出低8位 
            tmp = Integer.toHexString(j); 
            if (tmp.length() == 1) 
                sb.append("0"); 
            sb.append(tmp); 
            
        } 
        return (new String(sb)); 
    }
	
}
